const app = getApp()

Page({
  data:{
    current: [],
    history: [],
    active: 0,
    show: -1,
    contact: []
  },
  onShow() {
    // 未登录
    if(!app.globalData.logged) {
      console.log("用户未登录，加载失败！！！")
      return
    }
    var that = this
    const db = wx.cloud.database({
      env: 'cloud-dev-alpha-3f1k3'
    })
    wx.showLoading({
      title: '加载中',
    })
    db.collection('relation').get({
      success: res=>{
        wx.cloud.callFunction({
          name: 'queryUserCard',
          data: {
            cardsID: res.data
          },
          success: res=>{
            var cur = new Array()
            var his = new Array()
            var now = new Date()
            console.log(res)
            for(var i = 0; i < res.result.length; i++){
              if(new Date(res.result[i].offDate + ' ' + res.result[i].offTime) < now) {
                his.push(res.result[i])
              }
              else{
                cur.push(res.result[i])
              }
            }
            wx.hideLoading({
            })
            that.setData({
              current: cur,
              history: his
            })
            console.log("查询用户预约信息成功！")
          },
          fail: res=>{
            wx.showModal({
              title: '查询失败',
              content: '查询您的预约信息失败，你可以清理小程序后重新尝试'
            })
            console.log("查询用户预约信息失败！！！")
          }
        })
      },
      fail: res=>{
        wx.showModal({
          title: '获取失败',
          content: '小程序获取个人预约记录失败',
          success: res => {
            console.log("小程序端获取记录失败！！！")
          }
        })
      }
    })  
  },
  showDetial(event) {
    console.log(event)
    var arr = []
    if(!this.data.active){
      var arr = []
      for(var i = 0; i < this.data.current[event.currentTarget.id].phone.length; i++){
        var obj = {}
        var num = i + 1
        obj.name = '乘客' + num
        obj.subname = this.data.current[event.currentTarget.id].phone[i]
        arr.push(obj)
      }
      console.log(obj)
      this.setData({
        show: event.currentTarget.id,
        contact: arr
      })
    }
    else{
      var arr = []
      for(var i = 0; i < this.data.history[event.currentTarget.id].phone.length; i++){
        var obj = {}
        obj.name = '乘客' + i
        obj.subname = this.data.history[event.currentTarget.id].phone[i]
        arr.push(obj)
      }
      console.log(obj)
      this.setData({
        show: event.currentTarget.id,
        contact: arr
      })
    }
    
    console.log(obj)
    this.setData({
      show: event.currentTarget.id,
      contact: arr
    })
  },
  onClose() {
    this.setData({
      show: -1
    })
    console.log(this.data.show)
  },
  exitCard(event){
    console.log(event)
    var phone = wx.getStorageSync('phone')
    if(phone == '' || !phone) {
      wx.showModal({
        title: "添加失败",
        content: '您的手机号未填写，请在‘我的’页面填写后再试'
      })
    }
    const db = wx.cloud.database({
      env: 'cloud-dev-alpha-3f1k3'
    })
    wx.cloud.callFunction({
      name: 'exitGroup',
      data: {
        _id: event.currentTarget.id,
        phone: phone
      },
      success: res=>{
        db.collection('relation').where({
          cardID: event.currentTarget.dataset.cardid
        }).remove({
          success: res=>{
            console.log('退出成功！')
            wx.showToast({
              title: '退出成功',
              icon: 'none',
              duration: 2000
            })
          }
        })
      },
      fail: res=>{
        console.log('云函数修改卡片记录失败！！！')
        wx.showModal({
          title: "查找失败",
          content: "小程序没有找到您的这条记录"
        })
      }
    })
  }
})